package com.datamation.swadeshi.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.adapter.ListViewDataAdapter;
import com.datamation.swadeshi.model.ContentItem;


@SuppressLint("NewApi") 
public class SettingsActivity extends Activity {

	ArrayList<ContentItem> objects;

	@SuppressLint("Recycle") 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_settings_layout);
		
        //set title 
		setTitleBarColor();
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		TypedArray icons = getResources().obtainTypedArray(R.array.listView_icons_for_settingd);
		
		objects = new ArrayList<ContentItem>();

		objects.add(new ContentItem("Sync Configuration", "config url, server database and header database configuration", icons.getResourceId(0, -1)));
		objects.add(new ContentItem("Printer Configuration", "Enter your MAC address to connect", icons.getResourceId(1, -1)));
		objects.add(new ContentItem("Reschedule Uploading Details", "Please enter From date and To date", icons.getResourceId(2, -1)));
		objects.add(new ContentItem("SQLite Database", "DB backups and restore", icons.getResourceId(3, -1)));
		objects.add(new ContentItem("Sales Representative Details", "Reps informations", icons.getResourceId(4, -1)));
		objects.add(new ContentItem("Sales Rep Route", "Route area and code", icons.getResourceId(5, -1)));
		
		ListViewDataAdapter adapter = new ListViewDataAdapter(getApplicationContext(), objects);
	    
		ListView lv = (ListView) findViewById(R.id.settings_list_view);
		
		lv.setAdapter(adapter);
		
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view2,
					int position, long id) {
				
				String title=objects.get(position).getName().toString();
				Context context =SettingsActivity.this;
				
				switch (position) {
				case 0:
					syncDialogbox(context, title);
					Toast.makeText(getApplicationContext(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 1:
					printerDialogbox(context, title);
					Toast.makeText(getApplicationContext(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 2:
					rescheduleUploadDialogbox(context, title);
					Toast.makeText(getApplicationContext(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 3:
					sqliteDatabaseDialogbox(context, title);
					Toast.makeText(getApplicationContext(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 4:
					repsDetailsDialogbox(context, title);
					Toast.makeText(getApplicationContext(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				case 5:
					routeAreaDialogbox(context, title);
					Toast.makeText(getApplicationContext(), objects.get(position).getName() + " selected", Toast.LENGTH_SHORT).show();
					break;
				}

			}
		});
	}

	//Back  
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
		finish();
		return true;
		}
		return false;	
	}
	

	//Title bar color 
	@SuppressLint("NewApi") 
	private void  setTitleBarColor(){
		ActionBar bar = getActionBar();
		bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#006e17")));
		//title 
		createCutomActionBarTitle("System Configuration");		
	}
	@SuppressLint("NewApi") 
    private void createCutomActionBarTitle(String title){
        
    	this.getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.custom_action_bar, null);

        //Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/fat_tats.ttf");
        Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/coopbl.ttf");
        
        TextView tvTitle = (TextView)v.findViewById(R.id.titleFragment1);
        tvTitle.setText(title);
        tvTitle.setTypeface(tf);
      
        //assign the view to the actionbar
        this.getActionBar().setCustomView(v);
    }
	
	//Alerts DialogBox
	/**
	 * IP alert dialogbox
	 */
	@SuppressWarnings("unused")
	private void syncDialogbox(final Context context,String title){
		// get prompts.xml view
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.settings_sync_layout, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle(title);
		
		alertDialogBuilder.setView(promptView);

		EditText serverURL = (EditText) promptView.findViewById(R.id.et_server_url);
		Spinner serverDB = (Spinner) promptView.findViewById(R.id.spinner_server_db);
		Spinner headDB = (Spinner) promptView.findViewById(R.id.Spinner_head_db);
		// setup a dialog window
		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("Sync", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {

							//Do something
								
								
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});

		// create an alert dialog
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}

	/**
	 * Printer Config alert dialogbox
	 */
	@SuppressWarnings("unused")
	private void printerDialogbox(final Context context,String title){
		// get prompts.xml view
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.settings_printer_layout, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle(title);
		
		alertDialogBuilder.setView(promptView);

		EditText serverURL = (EditText) promptView.findViewById(R.id.et_mac_address);

		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							
								//Do something
							
								
								
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});
		
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}
	

	/**
	 * reschedule upload Config alert dialogbox
	 */
	@SuppressWarnings("unused")
	private void rescheduleUploadDialogbox(final Context context,String title){
		// get prompts.xml view
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.settings_reschedule_uploading_layout, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle(title);
		
		alertDialogBuilder.setView(promptView);

		EditText serverURL = (EditText) promptView.findViewById(R.id.et_mac_address);
		final DatePicker fromDatePicker= (DatePicker) promptView.findViewById(R.id.datePicker1);
		final DatePicker ToDatePicker= (DatePicker) promptView.findViewById(R.id.datePicker2);
		
		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							
								Log.v("Date", "From "+getDateFromDatePicker(fromDatePicker)+" To "+getDateFromDatePicker(ToDatePicker));
								
								
								
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});
		
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}
	
	@SuppressWarnings({ "deprecation", "unused" })
	public String getDateFromDatePicker(DatePicker datePicker){
		
		int   day  = datePicker.getDayOfMonth();
		int   month= datePicker.getMonth();
		int   year = datePicker.getYear();

		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		String formatedDate = sdf.format(new Date(year, month, day));
		
		return formatedDate;
		
		
	}
	
	/**
	 * SQLite Database Config alert dialogbox
	 */
	@SuppressWarnings("unused")
	private void sqliteDatabaseDialogbox(final Context context,String title){
		// get prompts.xml view
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.settings_sqlite_database_layout, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle(title);
		
		alertDialogBuilder.setView(promptView);
		
		final Button b_backups=(Button) promptView.findViewById(R.id.b_backups); 
		final Button b_restore=(Button) promptView.findViewById(R.id.b_restore); 
			
		b_backups.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("Backups", "OnClick");
			}
		});
		
		b_restore.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.v("Restore", "OnClick");
				Intent myIntent = new Intent(context,ImportActivity.class);
				startActivity(myIntent);
			}
		});
		
		
		alertDialogBuilder
				.setCancelable(false)
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});
		
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}

	/**
	 *Reps details alert dialogbox
	 */
	@SuppressWarnings("unused")
	private void repsDetailsDialogbox(final Context context,String title){
	
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.settings_reps_details_layout, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle(title);
		
		alertDialogBuilder.setView(promptView);

		
		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							
								
								
								
								
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});
		
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}

	/**
	 *Reps details alert dialogbox
	 */
	@SuppressWarnings("unused")
	private void routeAreaDialogbox(final Context context,String title){
	
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.settings_route_plan, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle(title);
		
		alertDialogBuilder.setView(promptView);

		
		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							
								
								
								
								
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								
							}
						});
		
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}
}
